#!/usr/bin/env python

import gtk
import gtk.glade
import pango

import gnomevfs

import os
import commands

import utils.IconFactory as uspIconFactory
import utils.ButtonFactory as uspButtonFactory
import utils.GConfigurator as uspGConfig
import uspCoreUtils 

from xdg.DesktopEntry import DesktopEntry

class SearchView(object):
	def __init__(self, widgetTree, mainWindow, mainEventWindow):
		self.searchBox = widgetTree.get_widget("RecentBox1")
		self.searchButton = widgetTree.get_widget("SearchButton1")
		self.searchButton.connect('clicked', self.onSearchButtonClicked)
		self.iconFactory = uspIconFactory.IconFactory()
		self.searchText = ''
		self.toolTips = gtk.Tooltips()
		
	def filterEntryChanged(self, text):
		self.searchText = text
	
	def fillRecentBox(self, searchResultPaths):
		uspCoreUtils.clearWidgetChildren(self.searchBox)
		buttonFactory = uspButtonFactory.ButtonFactory()
		buttonFactory.iconSize = gtk.ICON_SIZE_BUTTON
		buttonFactory.minLabelWidth = 20
		buttonFactory.labelAlignment = 0.5
		buttonFactory.toolTipWidget = self.toolTips
		
		buttonFactory.clearLabelAttributes()
		buttonFactory.addLabelAttribute(pango.AttrWeight(pango.WEIGHT_NORMAL, 0, -1))
		buttonFactory.addLabelAttribute(pango.AttrWeight(pango.WEIGHT_NORMAL, 0, -1))
		
		
		for path in searchResultPaths:
			if not os.path.exists(path):
				continue
			uri = gnomevfs.get_uri_from_local_path(path)
			buttonFactory.addEvent("button_release_event", self.onButtonClicked, uri)
			name = os.path.basename(path)
			button = buttonFactory.create([name], self.iconFactory.iconForURI(uri, 24), path)
			self.searchBox.pack_start(button, False, False, 0)
	
	def search(self):
		if len(self.searchText) < 3:
			return []
		result = commands.getoutput("slocate %s" % (self.searchText))
		return result.split()[:100]
		
	def onSearchButtonClicked(self, widget):
		result = self.search()
		self.fillRecentBox(result)
		
	def onButtonClicked(self, widget, event, uri):
		execute(['xdg-open', uri])
		
class Configuration(uspCoreUtils.Singleton):
	def __init__(self):
		uspCoreUtils.Singleton.__init__(self)
		
class UspPlugin(object):
	
	gladeFilename = "uspSearch.glade"
	
	def __init__(self):
		self.gladefile = os.path.join(os.path.dirname(__file__) , self.gladeFilename)
		
		if not os.path.exists(self.gladefile):
			raise Exception("Glade file '"+ self.gladeFilename +"' does not exist.")
			
		self.widgetTree = gtk.glade.XML(self.gladefile, "MainWindow")
		self.window = self.widgetTree.get_widget("MainWindow")
		self.contentHolder = self.widgetTree.get_widget("MainEventBox")
		
		self.gClient = uspGConfig.GConfigurator('/apps/usp2/plugins/search')
		Configuration().gClient = self.gClient
		
		self.searchView = SearchView(self.widgetTree, self.window, self.contentHolder)
	
	def pluginName(self):
		return "Search"
	
	def mainWidget(self):
		return self.contentHolder
	
	def pluginIcon(self):
		return 'gnome-system'
	
	def filterEntryChanged(self, text):
		self.searchView.filterEntryChanged(text)
	
	def getPluginSize(self):
		width = self.gClient.getInteger('width', 100)
		height = self.gClient.getInteger('height', 250)
		return width, height
	
	def setPluginSize(self, width, height):
		self.gClient.setInteger('width', width)
		self.gClient.setInteger('height', height)
